package com.qf.service;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsGoodsMapper;
import com.qf.pojo.DtsGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * projectName: wokepase1
 *
 * @author: 强力
 * time: 2021/9/13 17:01
 * description:
 */
@Service
public class GoodsServiceImpl implements GoodsService{
    @Autowired
    private DtsGoodsMapper goodsMapper;

    @Override
    public Integer goodcount() {
        QueryWrapper<DtsGoods> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("deleted",0);
        Integer count = goodsMapper.selectCount(queryWrapper);
        return  count;
    }

    @Override
    public IPage<DtsGoods> findByBage(Integer page, Integer limit, String goodsSn, String name, String sort, String order) {
        Page<DtsGoods> goodsPage=new Page<>(page,limit);

        QueryWrapper<DtsGoods> queryWrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty(goodsSn)){
            queryWrapper.like("`goods_sn`",goodsSn);
        }
        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        if (order.equalsIgnoreCase("asc")){
            queryWrapper.orderByAsc(sort);
        }else {
            queryWrapper.orderByDesc(sort);
        }

        Page<DtsGoods> pagelist = goodsMapper.selectPage(goodsPage, queryWrapper);

        return pagelist;
    }

    @Override
    public DtsGoods insert(DtsGoods goods) {

        goods.setAddTime(new Date());
        goods.setUpdateTime(new Date());
        goods.setDeleted(false);
        goodsMapper.insert(goods);
        return goods;
    }

    @Override
    public List<Map> selegoods() {
        List<Map> list = goodsMapper.selectOrder();
        return list;
    }
}
